package likou;

import java.util.Arrays;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/03/17 07:51
 */
public class _115 {
    public static void main(String[] args) {
        System.out.println(numDistinct("babgbag","bag"));
    }

    public static int numDistinct(String s, String t) {
        if (s.length() < t.length()) return 0;
        int m = s.length(), n = t.length();
        char[] ss = s.toCharArray();
        char[] tt = t.toCharArray();
        int[] bucket = new int[128];
        Arrays.fill(bucket, -1);
        int[] lastIndex = new int[n];
        for (int i = 0; i < n; i++) {
            lastIndex[i] = bucket[tt[i]];
            bucket[tt[i]] = i;
        }
        int[] dp = new int[n + 1];
        dp[0] = 1;
        for (int i = 1; i <= m; i++) {
            for (int j = bucket[ss[i - 1]]; j >= 0; j = lastIndex[j]) {
                dp[j + 1] += dp[j];
            }
        }
        return dp[n];
    }

}
